package crmdna.api.endpoint;

import java.util.List;

import javax.annotation.Nullable;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiMethod.HttpMethod;
import com.google.appengine.api.users.User;

import crmdna.client.Client;
import crmdna.common.APIResponse;
import crmdna.common.APIResponse.Status;
import crmdna.common.RequestInfo;
import crmdna.common.Utils;
import crmdna.inventory.InventoryItemType;
import crmdna.inventory.InventoryItemTypeProp;

@Api(name = "inventoryItemType")
public class InventoryItemTypeApi {
	@ApiMethod(path = "create", httpMethod = HttpMethod.POST)
	public APIResponse create(@Nullable @Named("client") String client,
			@Named("displayName") String displayName,
			@Nullable @Named("showStackTrace") Boolean showStackTrace,
			HttpServletRequest req, User user) {

		if (client == null)
			client = "isha";

		String login = null;

		try {
			Client.ensureValid(client);

			login = Utils.getLoginEmail(user);
			InventoryItemTypeProp prop = InventoryItemType.create(client, displayName, login);

			return new APIResponse(Status.SUCCESS, prop);

		} catch (Exception ex) {
			return Utils.toAPIResponse(ex, showStackTrace, new RequestInfo()
					.client(client).req(req).login(login));
		}
	}

	@ApiMethod(path = "getAll", httpMethod = HttpMethod.GET)
	public APIResponse getAll(@Nullable @Named("client") String client,
			@Nullable @Named("showStackTrace") Boolean showStackTrace,
			HttpServletRequest req) {

		if (client == null)
			client = "isha";

		try {
			Client.ensureValid(client);
			List<InventoryItemTypeProp> props = InventoryItemType.getAll(client);

			return new APIResponse(Status.SUCCESS, props);
		} catch (Exception ex) {
			return Utils.toAPIResponse(ex, showStackTrace, new RequestInfo()
					.client(client).req(req).login(null));
		}
	}

	@ApiMethod(path = "rename", httpMethod = HttpMethod.GET)
	public APIResponse rename(@Nullable @Named("client") String client,
			@Named("departmentId") long departmentId,
			@Named("newDisplayName") String newDisplayName,
			@Nullable @Named("showStackTrace") Boolean showStackTrace,
			HttpServletRequest req, User user) {
		
		if (client == null)
			client = "isha";

		try {
			Client.ensureValid(client);
			InventoryItemTypeProp prop = InventoryItemType.rename(client, departmentId,
					newDisplayName, Utils.getLoginEmail(user));

			return new APIResponse(Status.SUCCESS, prop);
		} catch (Exception ex) {
			return Utils.toAPIResponse(ex, showStackTrace, new RequestInfo()
					.client(client).req(req).login(null));
		}
	}

	@ApiMethod(path = "delete", httpMethod = HttpMethod.GET)
	public APIResponse delete(@Nullable @Named("client") String client,
			@Named("departmentId") long departmentId,
			@Nullable @Named("showStackTrace") Boolean showStackTrace,
			HttpServletRequest req, User user) {
		
		if (client == null)
			client = "isha";
		
		String login = null;

		try {
			Client.ensureValid(client);
			
			login = Utils.getLoginEmail(user);
			InventoryItemType.delete(client, departmentId, login);

			return new APIResponse(Status.SUCCESS, "Department [" + departmentId
					+ "] deleted");
			
		} catch (Exception ex) {
			return Utils.toAPIResponse(ex, showStackTrace, new RequestInfo()
					.client(client).req(req).login(login));
		}
	}
}
